﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Manufacturing;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_BOM : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public M_BOM()
        {
            InitializeComponent();
        }

        public void ShowSearch()
        {
            FND_BOM oForm = new FND_BOM();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            if (oForm.SelectedBOM != null)
            {
                txtBOMCode.Text = oForm.SelectedBOM;
                txtBOMName.Text = oForm.SelectedBOMName;

                BOMManager oManager = new BOMManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                DataTable _table = oManager.GetProductStructure(txtBOMCode.Text);
                DataRow oRow = _table.Rows[0];
                txtBOMCode.Enabled = false;
                chkInActive.Checked = (bool)oRow["IsActive"];
                txtBOMName.Text = oRow["BOMName"].ToString();
                txtItemCode.Text = oRow["ItemCode"].ToString();
                txtItemName.Text = oRow["ItemName"].ToString();
                txtQty.Text = oRow["Qty"].ToString();
                txtUoM.Text = oRow["UoM"].ToString();



                this.MaterialLineItems = oManager.GetProductStructureLineItems(txtBOMCode.Text);
                gridControl1.DataSource = this.MaterialLineItems;
                gridControl1.Refresh();

                if (gridView1.RowCount == 0)
                {
                    gridView1.AddNewRow();
                }

                oManager.Close();

                btnAdd.Text = "Update";
                
                txtItemCode.Properties.Buttons[0].Visible = false;
            }
        }

        void DisableComponents()
        {
            txtBOMCode.Enabled = false;
            txtBOMName.Enabled = false;
            txtItemCode.Enabled = false;
            txtItemName.Enabled = false;
            txtQty.Enabled = false;
            txtUoM.Enabled = false;

            btnAdd.Text = "Update";

        }
        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridView1.MoveLast();
            try
            {
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];
                string sItemCode = "";


                sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                if (sItemCode.Trim() == "")
                {
                    gridView1.DeleteRow(iCount);
                }
            }
            catch { }

        }
        DataTable GetLineItems()
        {
            DataTable _table = new DataTable();

            _table.Columns.Add("ItemGroup", typeof(string));
            _table.Columns.Add("ItemCode", typeof(string));
            _table.Columns.Add("ItemName", typeof(string));
            _table.Columns.Add("Qty", typeof(double));
            _table.Columns.Add("UoM", typeof(string));
            _table.Columns.Add("WhseCode", typeof(string));

            

            return _table;
        }
        void BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetWarehouse();
           
            cboWarehouse.DataSource = oTable;
            cboWarehouse.DisplayMember = "Dscription";
            cboWarehouse.ValueMember = "Code";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Dscription", "Dscription", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboWarehouse.Columns.Add(col);
            cboWarehouse.ShowHeader = false;
            
            oManager.Close();
        }
        public string ConnectionString { get; set; }
        public DataTable MaterialLineItems { get; set; }
        

        private void M_BOM_Load(object sender, EventArgs e)
        {
            BindWarehouse();
            #region Raw Materials
            this.MaterialLineItems = GetLineItems();

            gridControl1.DataSource = this.MaterialLineItems;
            gridControl1.Refresh();

            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }
            #endregion

        }

     
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITM_PRD_ONLY oForm = new FND_ITM_PRD_ONLY();
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();
            if (oForm.SelectedItemCode != null)
            {
                txtItemCode.Text = oForm.SelectedItemCode;
                txtItemName.Text = oForm.SelectedItemName;
                txtUoM.Text = oForm.SelectedUoM;
            }
        }

        private void txtSearchItemCode_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                #region New Code From Sales Order

                string _ItemGUID = System.Guid.NewGuid().ToString();
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                try
                {
                    string sItemCode = oButtonEdit.Text;
                    ItemManager oManager = new ItemManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dt = oManager.GetSalesItemByItemCode(sItemCode);

                    oManager.Close();

                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        #region If Has Rows

                        //_table.Columns.Add("ItemGroup", typeof(string));
                        //_table.Columns.Add("ItemCode", typeof(string));
                        //_table.Columns.Add("ItemName", typeof(string));
                        //_table.Columns.Add("Qty", typeof(double));
                        //_table.Columns.Add("UoM", typeof(string));
                        //_table.Columns.Add("IsFree", typeof(bool));


                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"], row["ItemCode"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"], row["Description"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UoM"], row["InvtyUntMsr"].ToString());

                        #endregion

                        gridView1.FocusedColumn = gridView1.Columns["Qty"];
                        gridView1.ShowEditor();

                        return;

                    }
                    else
                    {
                        if (sItemCode.Trim() != "")
                        {
                            MessageBox.Show("No item found in the Price List!!!", "Item Code", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            gridView1.FocusedColumn = gridView1.Columns["ItemCode"];

                            SendKeys.Send("{ENTER}");

                            return;
                        }

                    }
                }
                catch
                {
                }
                #endregion
            }


            if (e.KeyCode == Keys.Tab)
            {
                #region New Code From Sales Order

                string _ItemGUID = System.Guid.NewGuid().ToString();
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                try
                {
                    string sItemCode = oButtonEdit.Text;
                    ItemManager oManager = new ItemManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dt = oManager.GetSalesItemByItemCode(sItemCode);

                    oManager.Close();

                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        #region If Has Rows

                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"], row["ItemCode"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"], row["Description"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UoM"], row["InvtyUntMsr"].ToString());

                        #endregion

                        gridView1.FocusedColumn = gridView1.Columns["Qty"];
                        gridView1.ShowEditor();

                        return;

                    }
                    else
                    {
                        if (sItemCode.Trim() != "")
                        {
                            MessageBox.Show("No item found in the Price List!!!", "Item Code", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            gridView1.FocusedColumn = gridView1.Columns["ItemCode"];

                            SendKeys.Send("{ENTER}");

                            return;
                        }

                    }
                }
                catch
                {
                }
                #endregion
            }
        }

        private void txtSearchItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITM_BOM oForm = new FND_ITM_BOM();
            int iFocusRow = gridView1.FocusedRowHandle;
            oForm.ConnectionString = this.ConnectionString;
            oForm.ItemGroup = gridView1.GetRowCellValue(iFocusRow, gridView1.Columns["ItemGroup"]).ToString();
            oForm.ShowDialog();

            int iRowCount = gridView1.FocusedRowHandle;

            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["ItemCode"], oForm.SelectedItemCode);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["ItemName"], oForm.SelectedItemDescription);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["UoM"], oForm.SelectedItemUnitMeasure);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["Qty"], "0");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RemoveBlankLastRowFirst();

            BOMManager oManager = new BOMManager();
            BOMUnit oUnit = new BOMUnit();

           

            oUnit.BOMCode = txtBOMCode.Text;
            oUnit.BOMDescription = txtBOMName.Text;

            oUnit.ProductCode = txtItemCode.Text;
            oUnit.ProductDscription = txtItemName.Text;
            oUnit.Qty = txtQty.Text;
            oUnit.UoM = txtUoM.Text;
            oUnit.IsBOMActive = chkInActive.Checked;

            oUnit.BOMLineItems = this.MaterialLineItems;


            if (btnAdd.Text == "Add")
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                if (oManager.IsBOMExists(oUnit.BOMCode) == false)
                {
                    oManager.AddBOM(oUnit);
                }
                else
                {
                    MessageBox.Show("Product Structure is already exists!!!", "Product Structure", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                oManager.Close();

                btnAdd.Text = "Update";

                txtBOMCode.Enabled = false;
                
                

                MessageBox.Show("New Product Structure successfully added!!!", "Product Structure", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                oManager.UpdateBOM(oUnit);
                oManager.Close();
                
                txtBOMCode.Enabled = false;
                

                btnAdd.Text = "Update";

                MessageBox.Show("Product Structure successfully updated!!!", "Product Structure", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (gridView1.IsLastRow)
                {
                    string sItemCode = "";
                    int iCount = gridView1.FocusedRowHandle;

                    sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                    if (sItemCode != "")
                    {
                        gridView1.AddNewRow();

                        gridView1.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                        gridView1.FocusedColumn = gridView1.VisibleColumns[0];
                        gridView1.ShowEditor();
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ShowSearch();
        }
    }
}
